<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Template_Website extends Controller_Template
{
    public $template = 'template/website';

    /**
     * The before() method is called before your controller action.
     * In our template controller we override this method so that we can
     * set up default values. These variables are then available to our
     * controllers if they need to be modified.
     */
    public function before()
    {
        parent::before();

        if ($this->auto_render) {
            // Initialize empty values
            $this->template->title   = '';
            $this->template->content = '';
            $this->template->styles = array();
            $this->template->scripts = array();
        }

        $this->checkAcl();
    }

    /**
     * The after() method is called after your controller action.
     * In our template controller we override this method so that we can
     * make any last minute modifications to the template before anything
     * is rendered.
     */
    public function after()
    {
        if ($this->auto_render) {
            $styles = array(
                '/resources/css/style.css' => 'screen',
                '/resources/css/bootstrap.min.css' => 'screen',
                '/resources/css/bootstrap-fileupload.css' => 'screen',
                '/resources/css/uploadify.css' => 'screen'
            );
            $scripts = array(
                'http://code.jquery.com/jquery.min.js',
                '/resources/js/bootstrap.min.js',
                '/resources/js/bootstrap-fileupload.js',
                '/resources/js/base.js',
                '/resources/js/jquery.uploadify.min.js'
            );
            $this->template->styles = array_merge( $this->template->styles, $styles );
            $this->template->scripts = array_merge( $this->template->scripts, $scripts );

            // allgemeine Einstellungen laden
            $config = Kohana::$config->load('base')->get('default');
            $this->template->site = $config['site'];

            // Auth laden
//            $authInstance = Auth_ORM::instance();
            $authInstance = Auth::instance();
            $this->template->loggedIn = $authInstance->logged_in();

            // Userinfos laden
            $this->template->user = $authInstance->get_user();

        }
        parent::after();
    }

    /*
     * Checkt ob der User eingeloggt ist
     */
    public function checkAcl()
    {
        $controller = array('User');

        $requestController = $this->request->controller();

        if (in_array($requestController, $controller)) {

            $login = Auth::instance()->logged_in();

            if ($login) {
                //$user = Auth::instance()->get_user();
            } else {
                $this->redirect('welcome/index');
            }
        }
    }
}